#pragma once

#include <sys_reg.h>

typedef enum {
	SPI0_Q = 0x40013000,
	SPI1_Q = 0x40003800,
	SPI2_Q = 0x40003C00,
} spi_qe;

typedef enum {
	SPI_CTL0_R = 0x0,
	SPI_CTL1_R = 0x4,
	SPI_STAT_R = 0x8,
	SPI_DATA_R = 0xC,
	SPI_CRCPOLY_R = 0x10,
	SPI_RCRC_R = 0x14,
	SPI_TCRC_R = 0x18,
	SPI_I2SCTL_R = 0x1C,
	SPI_I2SPSC_R = 0x20,
} spi_re;

static inline u32_s spi_get_reg_q(spi_qe spi_q, spi_re spi_r, u32_s byte_r) {
	return spi_q + spi_r + byte_r;
}

static inline void spi_wreg(spi_qe spi_q, spi_re spi_r, u32_s value) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, 0);
	wreg(reg_q, value);
}

static inline void spi_wreg_u8(spi_qe spi_q, spi_re spi_r,
			       t8b_re byte_r, u32_s value) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, byte_r);
	wreg_u8(reg_q, value);
}

static inline void spi_wreg_u16(spi_qe spi_q, spi_re spi_r,
				t16b_re byte_r, u32_s value) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, byte_r);
	wreg_u16(reg_q, value);
}

static inline u32_s spi_rreg(spi_qe spi_q, spi_re spi_r) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, 0);
	return rreg(reg_q);
}

static inline u32_s spi_rreg_u8(spi_qe spi_q, spi_re spi_r, t8b_re byte_r) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, byte_r);
	return rreg_u8(reg_q);
}

static inline u32_s spi_rreg_u16(spi_qe spi_q, spi_re spi_r, t16b_re byte_r) {
	u32_s reg_q = spi_get_reg_q(spi_q, spi_r, byte_r);
	return rreg_u16(reg_q);
}
